using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

// boot manager template
public class BootGenerator : Singleton<BootGenerator>
{
    readonly string path = Application.dataPath + "/Scripts/Manager/BootMgr.cs";
    StringBuilder sb = new StringBuilder();
    public void UpdateProto(List<string> names)
    {
        string old_code = GetOldCode();
        string code = "";
        code += GetHeadCode(old_code,names);
        code += GetInitSystemModulesCode(old_code);
        code += GetInitNetModulesCode(old_code,names);

        code += GetEndCode();

        FileStream file = File.Open(path,FileMode.OpenOrCreate);
        file.SetLength(0);
        var bytes = Encoding.UTF8.GetBytes(code);
        file.Write(bytes,0,bytes.Length);
        file.Close();
    }

    public void UpdateSystemModule(string nameSpace,string className)
    {
        if(className.Trim() == "")
        {
            Debug.LogError("类名不能为空");
            return;
        }
        string old_code = GetOldCode();
        string code = "";
        code += GetHeadCode(old_code);
        code += GetInitSystemModulesCode(nameSpace,className,old_code);
        code += GetInitNetModulesCode(old_code);
        code += GetEndCode();

        FileStream file = File.Open(path,FileMode.OpenOrCreate);
        file.SetLength(0);
        file.Write(Encoding.UTF8.GetBytes(code),0,Encoding.UTF8.GetBytes(code).Length);
        file.Close();
    }

    string GetOldCode()
    {
        if(!File.Exists(path))
        {
            FileStream file = File.Create(path);
            file.Close();
        }

        return File.ReadAllText(path);
    }

    string GetHeadCode(string old_code = "",List<string> names = null)
    {
        sb.AppendLine("using UnityEngine;");
        sb.AppendLine("using System.Collections;");
        sb.AppendLine("using System.Collections.Generic;");

        // sb.AppendLine("//结束写入，命名空间不会覆盖");
        sb.AppendLine();
        sb.AppendLine("public class BootMgr : Singleton<BootMgr>");
        sb.AppendLine("{");
        sb.AppendLine("\tpublic override void Init()");
        sb.AppendLine("\t{");
        sb.AppendLine("\t\tbase.Init();");
        sb.AppendLine("\t\tInitSystemModules();");
        sb.AppendLine("\t\tInitNetModules();");
        sb.AppendLine("\t}");
        sb.AppendLine();
        
        string code = sb.ToString();
        sb.Clear();
        return code;
    }

    string GetInitNetModulesCode(string old_code = "",List<string> names = null)
    {
        string begin = "\t\t//开始写入网络模块的初始化";
        string end = "\t\t//结束写入网络模块的初始化";
        sb.AppendLine("\t//网络模块初始化");
        sb.AppendLine("\tvoid InitNetModules()");
        sb.AppendLine("\t{");
        sb.AppendLine(begin);
        var old_init = RegexMgr.Instance.RegexBeginAndEnd(old_code,begin,end);
        if(old_init != "")
        {
            sb.AppendLine("\t\t" + old_init.Trim());
            if(names != null && names.Count > 0) 
            {
                foreach (var item in names)
                {
                    if(!old_init.Contains(item))
                    {
                        sb.AppendLine($"\t\t{item}.{item}Net.Instance.Init();");
                    }
                }
            }
        }else
        {
            if(names != null && names.Count > 0) 
            {
                foreach (var item in names)
                {
                    sb.AppendLine($"\t\t{item}.{item}Net.Instance.Init();");
                }
            }
        }
        sb.AppendLine(end);

        sb.AppendLine("\t}");
        string code = sb.ToString();
        sb.Clear();
        return code;
    }

    string GetInitSystemModulesCode(string old_code = "")
    {
        string begin = "\t\t//开始写入系统模块的初始化";
        string end = "\t\t//结束写入系统模块的初始化";
        sb.AppendLine("\t//系统模块初始化");
        sb.AppendLine("\tvoid InitSystemModules()");
        sb.AppendLine("\t{");
        sb.AppendLine(begin);
        var old_init = RegexMgr.Instance.RegexBeginAndEnd(old_code,begin,end);
        if(old_init != "")
        {
            sb.AppendLine("\t\t" + old_init.Trim());
        }

        sb.AppendLine(end);

        sb.AppendLine("\t}");
        string code = sb.ToString();
        sb.Clear();
        return code;

    }

    string GetInitSystemModulesCode(string nameSpace,string className,string old_code = "")
    {
        string begin = "\t\t//开始写入系统模块的初始化";
        string end = "\t\t//结束写入系统模块的初始化";
        sb.AppendLine("\t//系统模块初始化");
        sb.AppendLine("\tvoid InitSystemModules()");
        sb.AppendLine("\t{");
        sb.AppendLine(begin);
        var old_init = RegexMgr.Instance.RegexBeginAndEnd(old_code,begin,end);
        if(old_init != "")
        {
            sb.AppendLine("\t\t" + old_init.Trim());

            if(nameSpace.Trim() != "")
            {
                if(!old_init.Contains($"{nameSpace}.{className}.Instance"))
                {
                    sb.AppendLine($"\t\t{nameSpace}.{className}.Instance.Init();");
                }
            }else{
                if(!old_init.Contains($"{className}.Instance"))
                {
                    sb.AppendLine($"\t\t{className}.Instance.Init();");
                }
            }
    
        }else
        {
            if(nameSpace.Trim() != "")
            {
                sb.AppendLine($"\t\t{nameSpace}.{className}.Instance.Init();");
            }else
            {
                sb.AppendLine($"\t\t{className}.Instance.Init();");
            }
        }
        sb.AppendLine(end);

        sb.AppendLine("\t}");
        string code = sb.ToString();
        sb.Clear();
        return code;
    }

    string GetEndCode()
    {
        // sb.AppendLine("\t}");
        sb.AppendLine("}");
        string code = sb.ToString();
        return code;
    }
}